<?php
class Page_Be_Cron_DuplicateUrls extends Page{
	function __construct(){
		parent::__construct();
	}
	public function defaultAction(){
		/* CONFIGURE THE MAIL */
		$mailer = new PHPMailer();
		$mailer->From = 'noreply@post.com';
		$mailer->AddAddress('alex.savca@sendmoments.de');
		$mailer->IsHTML(true);
		$duplicates = Db_Cms_Pages::findDuplicateUrls();
		$duplicateCounter = 0;
		if(count($duplicates)) {
			$adminProject = Db_Projects::findByField('pr_identifier', 'admin');
			$baseUrl = $adminProject->pr_domain_live;
			$message = '';
			$message .= '<ol>';
			foreach($duplicates as $duplicate){
				$message .= '<li>';
					$message .= "{$duplicate['url']}";
					$message .= '<ol>';
						if ($duplicate['p_dynamic_url']) {
							$paginationIds = explode(',', $duplicate['pagination_ids']);
							$pageIds = explode(',', $duplicate['page_ids']);
							$pageId = $pageIds[0];
							foreach ($paginationIds as $id) {
								$duplicateCounter++;
								$url = "{$baseUrl}cms/pages?action=addEditPagination&id={$pageId}&paginationId={$id}";
								$message .="<li><a href=\"$url\">$url</a></li>";
							}
						} else {
							$pageIds = explode(',', $duplicate['page_ids']);
							foreach ($pageIds as $pageId) {
								$duplicateCounter++;
								$url = "{$baseUrl}cms/pages?action=addEdit&id={$pageId}";
								$message .= "<li><a href=\"$url\">$url</a></li>";
							}
						}
					$message .= '</ol>';
				$message .= '</li>';
			}
			$message .= '</ol>';
			$mailer->Subject = "There are " . $duplicateCounter . " duplicate urls in your projects";
			$mailer->Body = $message;
			$mailer->Send();
		}

		return array();
	}
}